(function ($) {
    function DetailList(element, options) {
        this.dbPath = G_Path.path ? G_Path.path : "";
        this.element = element;
        this.contentElement;
        this.toolsElement;
        this.options = options;
        this.parentId = options.id;
        this.fieldInfo = this.options.fieldInfo;
        this.formName = "list";
        this.formIndex = 0;
        this.tableElement = null;
        this.data = [];
        this.start = 1;
        this.rowSize = 50;

        this.url = "/goods/cust/fields/json/" + this.parentId;
        this.deleteUrl = "/goods/cust/fields/delete";
        this.winDialog = null;
        this.status = this.options.status || {ADD: 1, EDIT: 2, VIEW: 3};
        this.dialogStatus = this.options.dialogStatus || {ADD: 1, EDIT: 2, VIEW: 3};
        this.highlightResult = function () {
            this.style.color = "#00f";
            this.style.backgroundColor = "#ffffd1";
            this.toolsbarButtons = [];
            this.nowUserId = "";
        };
        this.unhighlightResult = function () {
            this.style.color = "#000";
            this.style.backgroundColor = "transparent";
        };
        this.init();

    }

    DetailList.DEFAULTS = {
        title: "详细列表",
        fieldInfo: [
            {"field": "id", "title": "", "type": "hidden"},
            {"field": "fieldName", "title": "字段名称"},
            {"field": "fieldType", "title": "字段类型"},
            {"field": "fieldKey", "title": "字段关键字"},
            {"field": "remark", "title": "字段备注"},
        ]
    };
    $.extend(DetailList.prototype, {
        init: function () {
            var self = this;
            this.contentElement = $("<div></div>").appendTo(this.element);
            this.toolsElement = $("<div></div>").attr({
                "class": "toolbar"
            }).appendTo(this.element);

            $("<div class='btn-add'></div>").click(function () {
                self.showDialog();
            }).appendTo(this.toolsElement);

            this.get();
        },
        get: function () {
            var self = this;
            var url = this.url;
            var requestData = this.options.param ? this.options.param : {};
            $.get(url, requestData, function (data) {
                var queryPage = $.parseJSON(data);
                $(self.contentElement).html("");
                if (queryPage && queryPage.list) {
                    self.data = queryPage.list;
                }
                self.draw();
            });
        },
        draw: function () {
            var self = this;

            self.tableElement = document.createElement("table");
            self.tableElement.className = "tb-list table";
            var oRow = self.tableElement.insertRow(self.tableElement.rows.length);
            oRow.className = "tb_tr_title";

            if (this.options.showDelete || this.options.showEdit) {
                var oCell = oRow.insertCell(oRow.cells.length);
                oCell.innerHTML = "";
            }
            for (var j = 0; j < this.fieldInfo.length; j++) {
                var fi = this.fieldInfo[j];
                var oCell = oRow.insertCell(oRow.cells.length - 1);
                oCell.innerHTML = fi.title ? fi.title : "";
            }

            //系统已经有数据，放数据
            //添加数据
            for (var i = 0; i < this.data.length; i++) {
                self.addRow(this.data[i], i);
            }
            $(this.contentElement).append(self.tableElement);


        },
        addRow: function (model, i) {
            var self = this;
            var id;
            var oRow = self.tableElement.insertRow(self.tableElement.rows.length);
            if (model && model.id) {
                var id = model.id;
                oRow.setAttribute("id", id);
            }
            oRow.onmouseover = this.highlightResult;
            oRow.onmouseout = this.unhighlightResult;

            for (var j = 0; j < this.fieldInfo.length; j++) {
                var fi = this.fieldInfo[j];
                var oCell = oRow.insertCell(oRow.cells.length);
                var fieldValue = "";
                if (fi.field == "no") {
                    fieldValue = i + 1;
                }
                else {
                    fieldValue = model[fi.field];
                }
                if (fi.type && "img" == fi.type) {
                    fieldValue = "<img src='" + fieldValue + "' width='300' height='200'/>"
                }
                if (fi.type && "hidden" == fi.type) {
                    oCell.innerHTML = '<input type="hidden" name=list[' + this.formIndex + '].' + fi.field + ' value ="' + fieldValue + '"/>';
                } else {
                    fieldValue = fieldValue ? fieldValue : "";
                    oCell.innerHTML = fieldValue;
                    oCell.innerHTML = '<input type="text" name=list[' + this.formIndex + '].' + fi.field + ' value ="' + fieldValue + '"/>';
                }
            }
            if (this.options.showDelete) {
                var oCell = oRow.insertCell(oRow.cells.length);
                var button = document.createElement("button");
                button.type = "button"
                button.className = "btn";
                button.innerHTML = "删除";
                oCell.appendChild(button);
                button.setAttribute("docId", id);
                $(button).click(function (e) {
                    e.stopPropagation();
                    var id = $(this).attr("docId");
                    self.deleteDetail($(this), id);
                });

            }
            this.formIndex++;
        },
        show: function () {
            this.get();
        },
        deleteDetail: function (item, id) {
            var self = this;
            if (id) {
                var url = this.deleteUrl + "/" + id;
                dialog({
                    title: '确认消息',
                    content: '是否删除?',
                    width: 200,
                    ok: function () {
                        $.get(url, function (text) {
                            //先看数据库有没有，删除表格行
                            //self.get();
                            self.deleteTable(item, id);
                        });
                    },
                    cancel: function () {

                    }
                }).show();
            }
            else {
                self.deleteTable(item);
            }
        },
        deleteTable: function (item, id) {
            var oa = item[0];
            while (oa && oa.tagName != "TR") {
                oa = oa.parentNode;
            }
            this.tableElement.deleteRow(oa.rowIndex);
            this.refreshIndex();

        },
        refreshIndex: function () {
        },
        showDialog: function (flag, id) {
            this.addRow({});
        }

    });
    $.fn.formList = function (options) {
        var config = $.extend({}, DetailList.DEFAULTS, options);
        var detail = new DetailList(this, config);
        return detail;
    };


})(jQuery)